package com.geovis.emergency.spd.controller.system;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.constant.SwaggerConstants;
import com.geovis.common.core.controller.BaseController;
import com.geovis.emergency.spd.biz.system.service.ISystemParamService;
import com.geovis.emergency.spd.entity.system.entity.SystemParam;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemParamQueryDTO;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemParamSaveDTO;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemParamUpdateDTO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 系统管理模块-参数设置 前端控制器
 * </p>
 *
 * @author 王响
 * @since 2022-04-08
 */
@RestController
@RequestMapping("/system/systemParam")
@RequiredArgsConstructor
@Api(value = "系统管理模块-参数设置接口", tags = "系统管理模块-参数设置接口")
@Slf4j
@Validated
public class SystemParamController extends BaseController<ISystemParamService> {

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = SwaggerConstants.SAVE_VALUE, notes = SwaggerConstants.SAVE_NOTES)
    @PostMapping({"/save"})
    public Result save(@Validated @RequestBody SystemParamSaveDTO saveDTO) {
        baseService.save(saveDTO);
        return Result.ok();
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = SwaggerConstants.UPDATE_VALUE, notes = SwaggerConstants.UPDATE_NOTES)
    @PostMapping({"/update"})
    public Result update(@Validated @RequestBody SystemParamUpdateDTO updateDTO) {
        baseService.update(
                Wrappers.lambdaUpdate(SystemParam.class)
                        .set(SystemParam::getName,updateDTO.getName())
                        .set(SystemParam::getId,updateDTO.getId())
                        .set(SystemParam::getRemark,updateDTO.getRemark())
                        .set(SystemParam::getValue,updateDTO.getValue())
                        .eq(SystemParam::getId, updateDTO.getOldId())
        );
        return Result.ok();
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = SwaggerConstants.LIST_VALUE, notes = SwaggerConstants.LIST_NOTES)
    @PostMapping("/list")
    public Result<List<SystemParam>> list(@Validated @RequestBody SystemParamQueryDTO queryDTO) {
        return Result.ok(baseService.list(
                Wrappers.lambdaQuery(SystemParam.class)
                        .like(StrUtil.isNotBlank(queryDTO.getId()),SystemParam::getId,queryDTO.getId())
                        .like(StrUtil.isNotBlank(queryDTO.getValue()),SystemParam::getValue,queryDTO.getValue())
                        .like(StrUtil.isNotBlank(queryDTO.getRemark()),SystemParam::getRemark,queryDTO.getRemark())
                        .like(StrUtil.isNotBlank(queryDTO.getName()),SystemParam::getName,queryDTO.getName())
                        .orderByAsc(SystemParam::getId)));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = SwaggerConstants.GET_BY_ID_VALUE, notes = SwaggerConstants.GET_BY_ID_NOTES)
    @GetMapping("/getValueById/{id}")
    public Result<String> getValueById(@PathVariable("id") String id) {
        return Result.ok(baseService.getValueById(id));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = SwaggerConstants.REMOVE_BY_ID_LIST_VALUE, notes = SwaggerConstants.REMOVE_BY_ID_LIST_NOTES)
    @ApiImplicitParams({@ApiImplicitParam(name = "idList", value = "删除id的list", required = true, dataType = "java.util.Set", paramType = "body")})
    @PostMapping("/removeByIdList")
    public Result removeByIdList(@NotNull(message = "删除的id集合不能为空") @RequestBody Set<String> idList) {

        //baseService.removeByIdList(idList);
        return Result.failed("系统参数已使用，无法删除！");
    }

}
